package com.tps.cloud.system.controller;

import com.tps.cloud.group.UpdateGroup;
import com.tps.cloud.response.Result;
import com.tps.cloud.system.dto.SystemUserDto;
import com.tps.cloud.system.service.SystemUserService;
import com.tps.cloud.system.vo.SystemUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理 前端控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/users")
@Tag(name = "用户管理", description = "用于管理用户信息")
@Slf4j
public class SystemUserController {

    private final SystemUserService systemUserService;

    private final MessageSourceAccessor messages;

    @PostMapping
    @Operation(summary = "创建用户", description = "创建用户")
    public Result<Long> save(@RequestBody @Validated SystemUserDto systemUserDto) {

        log.info("----保存用户成功------");
        log.error("----保存用户错误------");
        log.debug("----跟踪保存用户------");
        //验证用户唯一
        SystemUserVo systemUserVo=systemUserService.findByUsername(systemUserDto.getUsername());
        if(systemUserVo!=null){
            return Result.failed(messages.getMessage("SystemUserDto.Username.Exist"));
        }
        Long id=systemUserService.createUser(systemUserDto);
        return Result.ok(id);
    }

    @PutMapping
    @Operation(summary = "更新用户信息", description = "更新用户信息")
    public Result<Boolean> update(@RequestBody @Validated({UpdateGroup.class}) SystemUserDto systemUserDto) {
        systemUserService.updateUser(systemUserDto);
        return Result.ok(true);
    }





    @GetMapping
    @Operation(summary = "根据ID获取用户信息", description = "根据用户ID查询用户的详细信息")
    @Parameter(name = "id", description = "用户id", required = true)
    public Result<SystemUserVo> getUser(@RequestParam("id") Long id) {
        SystemUserVo user = systemUserService.getUser(id);
        return Result.ok(user);
    }
}
